// pages/addcompany/addcompany.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false,

    name: '',
    address: '',
    personnum: '',
    phone: '',
    region: ['河北省', '保定市', '莲池区'],
    imageArray: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**
   * 添加图片
   */
  addImageBtnClick() {
    var that = this;
    if (that.data.imageArray.length >= 1) {

      wx.showToast({
        title: '最多选择一张图片',
        icon: 'none'
      })
      return;
    }

    var pics = that.data.imageArray;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) { // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          imageArray: pics.concat(tempFilePaths)
        })
        console.log(that.data.imageArray)
      }
    })
  },
  // 预览图集
  previewImage(e) {

    var that = this
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: that.data.imageArray[index],
      urls: that.data.imageArray

    });
  },
  //删除图片
  deleteImage(e) {
    var that = this
    var index = e.currentTarget.dataset.index;
    var imageArray = that.data.imageArray
    imageArray.splice(index, 1);
    that.setData({
      imageArray: imageArray
    })
  },

  /**
   * 更新户籍地
   */
  bindRegionChange: function(e) {

    this.setData({
      region: e.detail.value
    })
  },

  /**
   * 企业名称
   */
  inputName: function(e) {
    var that = this
    that.data.name = e.detail.value;
    console.log(that.data.name)
  },

  /**
   * 办公人数
   */
  inputPersonNum: function(e) {
    var that = this
    that.data.personnum = e.detail.value
  },

  /**
   * 联系方式
   */
  inputPhone: function(e) {
    var that = this
    that.data.phone = e.detail.value;
  },

  /**
   * 详细地址
   */
  inputAddress: function(e) {
    var that = this
    that.data.address = e.detail.value
  },

  /**
   * 提交
   */
  submit: function() {
    var that = this;
    if (that.data.name == '') {
      wx: wx.showToast({
        title: '请输入企业名称',
        icon: 'none'
      })
      return
    }
    if (that.data.personnum == '') {
      wx: wx.showToast({
        title: '请输入办公人数',
        icon: 'none'
      })
      return
    }
    if (that.data.phone == '') {
      wx: wx.showToast({
        title: '请输入联系方式',
        icon: 'none'
      })
      return
    }
    if (that.data.address == '') {
      wx: wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      })
      return
    }
    wx.showModal({
      title: '确认',
      content: '是否要提交企业信息',
      success: function(res) {
        if (res.confirm) {
          var model = {};
          model.openid = getApp().globalData.openId;
          model.name = that.data.name;
          model.area = that.data.region[0];
          model.area1 = that.data.region[1];
          model.area2 = that.data.region[2];
          model.area3 = that.data.address;
          model.people = that.data.personnum;
          model.contact = that.data.phone;

          wx.showLoading({
            title: '正在提交...',
          });

          if (that.data.imageArray.length > 0) {
            var imageSrc = that.data.imageArray[0]
            wx.uploadFile({
              url: getApp().BASE_URL + '/wxoffice/uploadOfficeOne',
              filePath: imageSrc,
              name: 'image',
              header: {
                'content-type': 'application/x-www-form-urlencoded',
              },
              success(res) {
                console.log(res)
                var data = JSON.parse(res.data)
                if (data.code == 200) {
                  var img = data.data
                  console.log(img)
                  model.file = img;
                  that.uploadModel(model)
                } else {
                  wx.hideLoading();
                  that.showErroMessage(res.data.msg)
                }
              },
              fail() {
                wx.hideLoading();
                wx.showToast({
                  title: '上传错误，请重试',
                  icon: 'none'
                })

              }
            })

          } else {
            model.file = '';
            that.uploadModel(model);
          }


        } else if (res.cancel) {
          console.log('用户点击了取消')
        }
      }
    })
  },

  /**
   * 上传数据
   */
  uploadModel: function(model) {
    console.log(model)
    var that = this;
    wx.request({
      url: getApp().BASE_URL + '/wxoffice/addMyOffice',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data: model,
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          wx.hideLoading();
          wx.showToast({
            title: '添加企业成功',
            icon: 'none'
          })
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)

        }
      },
      fail: function(res) {
        wx.hideLoading();
        that.showErroMessage(res.data.msg)
      },
      complete: function(res) {

      }
    })

  },

  /**
   * 显示信息
   */
  showErroMessage: function(mes) {
    wx.showToast({
      title: mes,
      icon: 'none',
      duration: 1500
    })
  }


})